﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using RWProxyDemo.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RWProxyDemo.Domains
{
   


    /// <summary>
    /// 结果
    /// </summary>
    public class Result<T>
    {
        /// <summary>
        /// 操作状态
        /// </summary>
        public State State { get; set; } = State.Succeed;

        /// <summary>
        /// 结果
        /// </summary>
        public T Data { get; set; }

      


        /// <summary>
        /// ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString(){
            /*var setting = new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
            };
            setting.Converters.Add(new IsoDateTimeConverter {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
             });
            return JsonConvert.SerializeObject(this, setting );*/

            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            using (NullJsonWriter njw = new NullJsonWriter(sw))
            {
                JsonSerializer ser = new JsonSerializer();
                ser.Formatting = Formatting.Indented;
                ser.NullValueHandling = NullValueHandling.Include;
                ser.Converters.Add(new IsoDateTimeConverter
                {
                    DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
                });
                ser.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
                ser.Serialize(njw, this);
            }

            return sb.ToString();
        }
    }

    /// <summary>
    /// 状态
    /// </summary>
    public enum State
    {
        ///// <summary>
        ///// 未知
        ///// </summary>
        //Unknown = 0,

        /// <summary>
        /// 成功
        /// </summary>
        [Description("成功")]
        Succeed = 1,

        /// <summary>
        /// 失败
        /// </summary>
        Fail = 2,
    }

    /// <summary>
    /// 错误信息
    /// </summary>
    public class Error
    {
        /// <summary>
        /// 错误编码
        /// </summary>
        public string ErrorCode { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string ErrorMessage { get; set; }
    }

    ///// <summary>
    ///// 错误编码
    ///// ErrorCode需要具体接口具体定义，建议的格式是：服务号_业务号_具体错误编码号，
    ///// 比如:用户角色权限项目服务号为500，用户Controllers号为001，修改用户时，因为用户名不合法导致失败，为001，那么此时的ErrorCode应该为：500001001,
    ///// ErrorMessage即是：用户名不合法，请重新编辑！
    ///// 但要考虑到一些特殊情况，比如拦截到的全局系统异常，因此可以将所有系统级别的错误码统一设置为000，比如500000001
    ///// </summary>
    //public enum ErrorCode
    //{
    //    /// <summary>
    //    /// 系统异常
    //    /// </summary>
    //    [Description("连接异常，请稍后重试！")]
    //    System_Error = 100000001,

    //    /// <summary>
    //    /// 用户信息不能为空
    //    /// </summary>
    //    [Description("用户信息不能为空！")]
    //    User_Null = 100001001,

    //}
}
